package device_ctr;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class Connect_server extends  Thread {
	@Override
    public void run() {
		Looper.prepare();
		 Handler mChildHandler = new Handler() {
             @Override
             public void handleMessage(Message msg) {
            	 tongxin((ArrayList)msg.obj);
             }
         };
        Looper.loop();
	}
	private void tongxin(ArrayList params){
		String httpUrl="http://36.55.231.19/debug";
		
		String result = new String();
		HttpPost httpRequest = new HttpPost(httpUrl);
		try {
			HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
			httpRequest.setEntity(entity);
			HttpClient client = new DefaultHttpClient();
			client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
			HttpResponse response = client.execute(httpRequest);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				InputStream in = response.getEntity().getContent();
				Reader  inputStreamReader = new InputStreamReader(in); 
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader,8192);  
				
				String s;    
				  while((s = bufferedReader.readLine())!=null){    
					  result += s;    
				  } 
			}
			//textView.setText("123");
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println( e.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println( e.toString());
		}
		 System.out.println( result);
	}
}
